package com.ntanh.mealplanner.base.model;

import android.content.ContentValues;
import android.util.Log;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Anh Nguyen on 2/24/2015.
 */
public abstract class NamedEntity extends BaseEntity {

    public static final String NAME = "name";

    public static final String IMAGE = "image";

    public static final String DESCRIPTION = "description";

    protected String name;

    protected String image;

    protected String description;

    public NamedEntity(String author, String name, String image, String description) {
        super(author);
        this.name = name;
        this.image = image;
        this.description = description;
    }

    public NamedEntity(String author, String name) {
        super(author);
        this.name = name;
    }

    public NamedEntity(String author) {
        super(author);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof NamedEntity)) return false;
        if (!super.equals(o)) return false;

        NamedEntity that = (NamedEntity) o;

        if (description != null ? !description.equals(that.description) : that.description != null)
            return false;
        if (image != null ? !image.equals(that.image) : that.image != null) return false;
        if (name != null ? !name.equals(that.name) : that.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (image != null ? image.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        return result;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public JSONObject toJSONObject() {

        JSONObject json = super.toJSONObject();
        try {
            if (json != null) {
                json = new JSONObject();
            }
            json.put(NamedEntity.NAME, name);
            json.put(NamedEntity.IMAGE, image);
            json.put(NamedEntity.DESCRIPTION, description);

        } catch (Exception e) {
            Log.d("Convert error", "Cannot convert NamedEntity to JSON", e);
        }

        return json;
    }

    public void toContentValue(ContentValues values, boolean forInserting) {
        super.toContentValue(values, forInserting);
        values.put(NAME, getName());
        values.put(DESCRIPTION, getDescription());
        values.put(IMAGE, getImage());
    }
}
